package com.pointi.emoConverterM;

import java.io.BufferedInputStream;
//import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
import com.pointi.emoGatewayM.JDBC;
import com.pointi.emoTools.emoCheckList;
import com.pointi.emoTools.emoReceived;

public class ThreadConverter extends Thread
{
	//____________________________________________________________________________________________________
	// DEF
	
	private ConcurrentLinkedQueue<ThreadConverter> activeThreads ;
	private String filePathOrg ;
	private String fileNameOrg ;
	private String filePathTgt ;
	//private String fileNameTgt ;
	private int callkey ;
	private JDBC jdbc ;

	//____________________________________________________________________________________________________
	// INIT
	
	public ThreadConverter(ConcurrentLinkedQueue<ThreadConverter> inThread)
	{
		activeThreads = inThread ;
	}

	public void start(JDBC jdbc, int callkey, emoReceived received)
	{
		this.jdbc = jdbc ;
		this.callkey = callkey ;
		this.filePathOrg = received.fnGetOriginalPath() ;
		this.fileNameOrg = received.fnGetOriginalName() ;
		this.filePathTgt = received.fnGetTargetPath() ;
		//this.fileNameTgt = received.fnGetTargetName() ;
		this.activeThreads.add(this) ;
		super.start() ;
	}
	
	//____________________________________________________________________________________________________
	// RUN // Converter

	public void run()
	{
		//____________________________________________________________________________________________________
		// INIT
		
		FileInputStream fis = null ;
		//FileOutputStream fos = null ;
		BufferedInputStream bis = null ;
		//BufferedOutputStream bos = null ;
		
		//____________________________________________________________________________________________________
		// Get File // t_recfile_info.callkey  / file_path_tgt / file_name_tgt

		File file = new File(filePathOrg + fileNameOrg) ;
		int mFileLength = (int)file.length() ;
		byte [] inContainer = new byte [mFileLength] ;

		int frameLength = 100 ;
		int SP = 0 ;
		int CP = frameLength ;
		int EP = mFileLength ;

		try
		{
			fis = new FileInputStream(file) ;
			bis = new BufferedInputStream(fis) ;
			while (SP < EP)
			{
				if ((SP + CP) > EP) CP = EP - SP ;
				bis.read(inContainer, SP, CP) ;
				SP += CP ;
			}
			bis.close() ;
			fis.close() ;
		}
		catch (IOException e) { e.printStackTrace() ; }

		//____________________________________________________________________________________________________
		// Convert & Check // t_checklist_yyyymm.check_converter

		fnCheckDirectory() ;
		//File outFile = new File(filePathTgt + fileNameTgt) ;

		try
		{
			//fos = new FileOutputStream(outFile) ;
			//bos = new BufferedOutputStream(fos) ;
		}
		catch (Exception e) { e.printStackTrace() ; }

		try
		{
			// Convert Start
			jdbc.fnUpdateCheckList(emoCheckList.mCheckConverter, emoCheckList._START, callkey) ;
			
			//while (loopHandler)
			//{
				// Converting
				jdbc.fnUpdateCheckList(emoCheckList.mCheckConverter, emoCheckList._ON, callkey) ;
				// NOTE Converting // Compressed(ByteArray) > PCM(ByteArray) > Save(File)
				//System.out.println("[GW][CVT] ( ( ( ((( Converting ))) ) ) )") ;
			//}

			// NOTE Get Converted Data
			//byte [] outContainer = inContainer ;
			int mConvertedDataLength = mFileLength ;
			
			SP = 0 ;
			CP = frameLength ;
			EP = mConvertedDataLength ;
			while (SP < EP)
			{
				// Save Converted Data
				if ((SP + CP) > EP) CP = EP - SP ;
				//bos.write(outContainer, SP, CP) ;
				//bos.flush() ;
				SP += CP ;
			}

			// Convert End
			jdbc.fnUpdateCheckList(emoCheckList.mCheckConverter, emoCheckList._END, callkey) ;
			//bos.close() ;
			//fos.close() ;
		}
		//catch (IOException e) { e.printStackTrace() ; }
		catch (Exception e) { e.printStackTrace() ; }
		//System.out.println("[GW][CVT] Pop Converting Job | " + fileNameOrg) ;
		activeThreads.remove(this) ;
	}
	
	//____________________________________________________________________________________________________
	// Prepare Directory
	
	private void fnCheckDirectory()
	{
        String fileTargetPath = filePathTgt ;
        File dir = new File(fileTargetPath) ;
         if (!dir.exists()) { dir.mkdirs() ; }
	}
	
	//____________________________________________________________________________________________________
	// EOC
}
